<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>17.Math.random随机点名操作</title>
</head>

<body>
  <script>
    //0<= ~ <1
    console.log(Math.random());
    //返回0~5的随机数，不包括5  因为最大随机出4.5，floor向下取整就是0～4
    console.log(Math.floor(Math.random() * 5));
    //0～5随机数包括5: 公式 Math.floor(Math.random() * (MAX+1))
    console.log(Math.floor(Math.random() * (5 + 1)));
    //MIN~MAX 区间随机数： MIN+ Math.floor(Math.random() * (MAX -Min+1)),不包含Max就不必+1
    console.log(2 + Math.floor(Math.random() * (5 - 2 + 1)));
    //数组随机元素
    const students = ['小明', '张三', '李四', '王五', '赵六'];
    console.log(students[Math.floor(Math.random() * students.length)]);
    //2~5
    console.log(students[2 + Math.floor(Math.random() * (students.length - 2))]);
    //2~3
    console.log(students[2 + Math.floor(Math.random() * (3 - 2 + 1))]);

    function arrayRandomValue (array, start = 1, end) {
      end = end ? end : array.length;
      start --;
      const index = start + Math.floor(Math.random() * (end - start));
      return array[index];
    }
    console.log(arrayRandomValue(students,2,3));
  </script>
</body>

</html>